/*
 *  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.material.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MaterialCurrencyException;
import com.pitop.material.dao.MaterialCurrencyDAO;
import com.pitop.material.entity.MaterialCurrencyState;
import com.pitop.material.entity.MaterialCurrency;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("materialCurrencySV")
@Slf4j
public class MaterialCurrencySVImpl extends BaseSVImpl<MaterialCurrency, Long> implements MaterialCurrencySV {

    @Autowired
    private MaterialCurrencyDAO materialCurrencyDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return materialCurrencyDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(MaterialCurrency entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        super.save(entity);
    }

    /**
     * 加载一个对象MaterialCurrency
     * @param id * @param code 编码
     * @return MaterialCurrency
     */
    @Override
    public MaterialCurrency load(java.lang.Long id,java.lang.String code) {
           if(id==null&&code==null){
                 throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            return materialCurrencyDAO.load(param);
    }
    /**
     * 加载一个对象MaterialCurrency 通过id
     * @param id 
     * @return MaterialCurrency
     */
    @Override
    public MaterialCurrency loadById(java.lang.Long id) {
            if(id==null){
                 throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return materialCurrencyDAO.loadById(id);
    }

    /**
     * 加载一个对象MaterialCurrency 通过code
     * @param code 编码
     * @return MaterialCurrency
     */
    @Override
    public MaterialCurrency loadByCode(java.lang.String code) {
            if(code==null){
                 throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return materialCurrencyDAO.loadByCode(code);
    }


    /**
    * 根据主键id,oldStates 共同更新 MaterialCurrency 的状态到newState状态
    *
    * @param id 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateById(java.lang.Long id,MaterialCurrencyState newState,MaterialCurrencyState... oldStates){
        if(id==null){
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        materialCurrencyDAO.updateStateById(id,new Date(),newState.name(), states);
    }
    /**
    * 根据主键code,oldStates 共同更新 MaterialCurrency 的状态到newState状态
    *
    * @param code 编码
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByCode(java.lang.String code,MaterialCurrencyState newState,MaterialCurrencyState... oldStates){
        if(code==null){
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        materialCurrencyDAO.updateStateByCode(code,new Date(),newState.name(), states);
    }

    /**
    * 根据主键id 更新 MaterialCurrency 的状态到另一个状态
    *
    * @param id 
    * @param state 状态
    */
    @Override
    public void updateById(java.lang.Long id,MaterialCurrencyState state){
            if(id==null){
               throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          materialCurrencyDAO.updateById(id,state.name(),new Date());
    }
    /**
    * 根据主键code 更新 MaterialCurrency 的状态到另一个状态
    *
    * @param code 编码
    * @param state 状态
    */
    @Override
    public void updateByCode(java.lang.String code,MaterialCurrencyState state){
            if(code==null){
               throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          materialCurrencyDAO.updateByCode(code,state.name(),new Date());
    }


    /**
     * 删除对象MaterialCurrency
     * @param id * @param code 编码
     * @return MaterialCurrency
     */
    @Override
    public void delete(java.lang.Long id,java.lang.String code) {
            if(id==null&&code==null){
                throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            materialCurrencyDAO.delete(param);
    }



    /**
    * 查询MaterialCurrency分页
    *
    * @param materialCurrency  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<MaterialCurrency>
    */
    @Override
    public List<MaterialCurrency> list(MaterialCurrency materialCurrency, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (materialCurrency != null) {
               map = JSON.parseObject(JSON.toJSONString(materialCurrency, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return materialCurrencyDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(MaterialCurrency materialCurrency){
        Map<String, Object> map = null;
        if (materialCurrency != null) {
           map = JSON.parseObject(JSON.toJSONString(materialCurrency, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return materialCurrencyDAO.count(map);
     }

    /**
     * 查询MaterialCurrency分页
     *
     * @param id  * @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<MaterialCurrency>
     */
    @Override
    public List<MaterialCurrency> list(java.lang.Long id,java.lang.String code,int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            return materialCurrencyDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id,java.lang.String code) {
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            return materialCurrencyDAO.count(param);
    }

}
